package com.fernfx.xingtan.my.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.my.contract.PrivateSettingContract;
import com.fernfx.xingtan.my.entity.GetPrivateSettingEntity;
import com.fernfx.xingtan.my.presenter.PrivateSettingPresenter;
import com.fernfx.xingtan.utils.LogUtils;
import com.fernfx.xingtan.utils.OtherUtil;
import com.suke.widget.SwitchButton;

import butterknife.BindView;

/**
 * Created by admin on 2018/8/16.
 * 隐私设置
 */

public class PrivateSettingActivity extends BaseActivity implements SwitchButton.OnCheckedChangeListener, PrivateSettingContract.View {
    @BindView(R.id.add_account_bt)
    SwitchButton addAccountBt;
    @BindView(R.id.add_calling_card_bt)
    SwitchButton addCallingCardBt;
    @BindView(R.id.add_pnumber_bt)
    SwitchButton addPnumberBt;
    @BindView(R.id.account_fd_bt)
    SwitchButton fdAccountBt;
    @BindView(R.id.pnumber_fd_bt)
    SwitchButton fdPnumberBt;
    //init handler
    private Handler listenerHandler = initListenerHandler();

    private PrivateSettingContract.Presenter presenter;
    private int changeChecked = 2;

    public static void start(Context context) {
        Intent intent = new Intent(context, PrivateSettingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_setting_private;
    }

    @Override
    protected void initData() {
        requestArgsMap.clear();
        presenter.request(requestArgsMap);
    }

    @Override
    protected void initView() {
        presenter = new PrivateSettingPresenter();
        presenter.init(this);
        setToolbarTitle("隐私");
    }

    @Override
    protected void initListener() {

    }

    private int lastViewId = 0;
    private boolean lastIsChecked;

    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        if (view == null || view.getTag() == null) {
            return;
        }
        if (lastViewId == view.getId() && lastIsChecked == isChecked) {
            return;
        }else {
            //赋值
            lastViewId = view.getId();
            Message msg = listenerHandler.obtainMessage();
            msg.what = changeChecked;
            msg.obj = isChecked+"";
            listenerHandler.sendMessageDelayed(msg,400);
        }
        String settingResult = isChecked ? "0" : "1";
        requestArgsMap.clear();
        requestArgsMap.put("settingResult",settingResult);
        switch (view.getId()) {
            case R.id.add_account_bt:
                requestArgsMap.put("type","1");
                presenter.savePrivacySetting(requestArgsMap,view);
                break;
            case R.id.add_calling_card_bt:
                requestArgsMap.put("type","2");
                presenter.savePrivacySetting(requestArgsMap,view);
                break;
            case R.id.add_pnumber_bt:
                requestArgsMap.put("type","3");
                presenter.savePrivacySetting(requestArgsMap,view);
                break;
            case R.id.account_fd_bt:
                requestArgsMap.put("type","4");
                presenter.savePrivacySetting(requestArgsMap,view);
                break;
            case R.id.pnumber_fd_bt:
                requestArgsMap.put("type","5");
                presenter.savePrivacySetting(requestArgsMap,view);
                break;
        }
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }

    @Override
    public void showSettingInfo(GetPrivateSettingEntity.ObjBean objBean) {
        if (objBean == null) {
            return;
        }
        addAccountBt.setOnCheckedChangeListener(null);
        addCallingCardBt.setOnCheckedChangeListener(null);
        addPnumberBt.setOnCheckedChangeListener(null);
        fdAccountBt.setOnCheckedChangeListener(null);
        fdPnumberBt.setOnCheckedChangeListener(null);
        //根据后台返回的记录设置状态
        addAccountBt.setChecked(OtherUtil.getStatusFlag(objBean.getAddByAccount()));
        addAccountBt.setTag("1");

        addCallingCardBt.setChecked(OtherUtil.getStatusFlag(objBean.getAddByBussiness()));
        addCallingCardBt.setTag("2");

        addPnumberBt.setChecked(OtherUtil.getStatusFlag(objBean.getAddByMobile()));
        addPnumberBt.setTag("3");

        fdAccountBt.setChecked(OtherUtil.getStatusFlag(objBean.getSearchByAccount()));
        fdAccountBt.setTag("4");

        fdPnumberBt.setChecked(OtherUtil.getStatusFlag(objBean.getSearchByMobile()));
        fdPnumberBt.setTag("5");

        listenerHandler.sendMessageDelayed(listenerHandler.obtainMessage(),400);
    }

    public Handler initListenerHandler(){
        final SwitchButton.OnCheckedChangeListener listener = this;
        return  new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (changeChecked == msg.what) {
                    lastIsChecked = Boolean.parseBoolean(msg.obj.toString());
                }else {
                    addAccountBt.setOnCheckedChangeListener(listener);
                    addCallingCardBt.setOnCheckedChangeListener(listener);
                    addPnumberBt.setOnCheckedChangeListener(listener);
                    fdAccountBt.setOnCheckedChangeListener(listener);
                    fdPnumberBt.setOnCheckedChangeListener(listener);
                }
            }
        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
            presenter = null;
        }
        if (listenerHandler != null) {
            listenerHandler = null;
        }
    }
}
